﻿// Copyright (c) 2014 Converter Systems LLC

using ConverterSystems.ServiceModel;
using ConverterSystems.Workstation.Infrastructure;
using Opc.Ua.Client;
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;

namespace ConverterSystems.Workstation.ViewModels
{
    /// <summary>
    /// A view model for AxisView. Shows how to pass parameters to the view model.
    /// </summary>
    /// <remarks>
    /// In this example, two query parameters are passed in the navigation url:
    /// startNodeId := the starting nodeId which determines which object you wish to subscribe
    /// displayName := a string to display in the view.
    /// </remarks>
    [Export]
    public class AxisViewModel : NavigationViewModelBase
    {
        private TestServerSession _session;
        private Subscription _subscription;

        [ImportingConstructor]
        public AxisViewModel(TestServerSession session)
        {
            if (session == null) throw new ArgumentNullException("session");
            _session = session;
        }

        public override void OnNavigatedTo(Microsoft.Practices.Prism.Regions.NavigationContext navigationContext)
        {
            if (_subscription != null) return;
            // Create a new subscription, optionally specifying a faster publishing interval in milliseconds.
            _subscription = new Subscription(_session.DefaultSubscription) { PublishingInterval = 250 };
            // Configure this subscription with bindings to the properties specified with a [MonitoredItem] attribute.
            _subscription.WithModel(this).AddBindings(navigationContext.Parameters["startNodeId"] as string);
            // Add the new subscription to the session
            _session.AddSubscription(_subscription);
            DisplayName = navigationContext.Parameters["displayName"] as string;
        }

        public override void OnNavigatedFrom(Microsoft.Practices.Prism.Regions.NavigationContext navigationContext)
        {
            if (_subscription == null) return;
            _session.RemoveSubscription(_subscription);
            _subscription.Dispose();
            _subscription = null;
        }

        /// <summary>
        /// Gets or sets the value of Axis.
        /// omit the StartNodeId to use the string passed in the NavigationParameters
        /// optionally include a RelativePath in the form of "browseName", or "browseName1/browseName2..."
        /// </summary>
        [MonitoredItem()]
        public Single Axis
        {
            get { return _Axis; }
            set { SetProperty(ref _Axis, value); }
        }

        private Single _Axis;

        /// <summary>
        /// Gets the DisplayName.
        /// </summary>
        public string DisplayName
        {
            get { return _DisplayName; }
            set { SetProperty(ref _DisplayName, value); }
        }

        private string _DisplayName;
    }
}